﻿using Little.Soldier.SWIFT.Validation;
using Little.Soldier.SWIFT.Validation.Block2;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Little.Soldier.SWIFT
{
  [Serializable]
  public class SwiftBlock2Output : SwiftBlock2
  {
    private MessageInputReference _MessageInputReference = new MessageInputReference();
    private string _OutputDate;
    private string _OutputTime;
    private string _InputTime;

    public virtual string ReceiverOutputDate
    {
      get
      {
        return this._OutputDate;
      }
      set
      {
        this._OutputDate = value;
      }
    }

    public virtual string ReceiverOutputTime
    {
      get
      {
        return this._OutputTime;
      }
      set
      {
        this._OutputTime = value;
      }
    }

    public virtual string InputTime
    {
      get
      {
        return this._InputTime;
      }
      set
      {
        this._InputTime = value;
      }
    }

    public override bool IsEmpty
    {
      get
      {
        switch (this.MessageType)
        {
          case null:
            if (this.InputTime == null && (this.MessageInputReference != null && this.MessageInputReference.GetValue() == null) && (this.ReceiverOutputDate == null && this.ReceiverOutputTime == null))
              return this.MessagePriority == null;
            else
              break;
        }
        return false;
      }
    }

    public override bool IsInput
    {
      get
      {
        return false;
      }
    }

    public virtual MessageInputReference MessageInputReference
    {
      get
      {
        return this._MessageInputReference;
      }
      set
      {
        this._MessageInputReference = value;
      }
    }

    public SwiftBlock2Output(SwiftMessage swiftMessage)
      : base(swiftMessage)
    {
    }

    public SwiftBlock2Output(SwiftMessage swiftMessage, string messageType, string senderInputTime, string messageInputReference_InputDate, string messageInputReference_LTAddress, string messageInputReference_SessionNumber, string messageInputReference_SequenceNumber, string receiverOutputDate, string receiverOutputTime, string messagePriority)
      : base(swiftMessage)
    {
      this.MessageType = messageType;
      this.InputTime = senderInputTime;
      this.ReceiverOutputDate = receiverOutputDate;
      this.ReceiverOutputTime = receiverOutputTime;
      this.MessagePriority = messagePriority;
      this.MessageInputReference.InputDate = messageInputReference_InputDate;
      this.MessageInputReference.LTAddress = messageInputReference_LTAddress;
      this.MessageInputReference.SessionNumber = messageInputReference_SessionNumber;
      this.MessageInputReference.SequenceNumber = messageInputReference_SequenceNumber;
    }

    public override void InitializeRules()
    {
      this.Rules.Add((IValidate) new Validate_InputTime(typeof (SwiftBlock2Output).GetProperty("InputTime"), (object) this));
      this.Rules.Add((IValidate) new Validate_InputDate(typeof (MessageInputReference).GetProperty("InputDate"), (object) this.MessageInputReference));
      this.Rules.Add((IValidate) new Validate_LTAddress(typeof (MessageInputReference).GetProperty("LTAddress"), (object) this.MessageInputReference));
      this.Rules.Add((IValidate) new Validate_SessionNumber(typeof (MessageInputReference).GetProperty("SessionNumber"), (object) this.MessageInputReference));
      this.Rules.Add((IValidate) new Validate_SequenceNumber(typeof (MessageInputReference).GetProperty("SequenceNumber"), (object) this.MessageInputReference));
      this.Rules.Add((IValidate) new Validate_OutputDate(typeof (SwiftBlock2Output).GetProperty("ReceiverOutputDate"), (object) this, true));
      this.Rules.Add((IValidate) new Validate_OutputTime(typeof (SwiftBlock2Output).GetProperty("ReceiverOutputTime"), (object) this, true));
      this.Rules.Add((IValidate) new Validate_MessagePriority(typeof (SwiftBlock2).GetProperty("MessagePriority"), (object) this, true));
      base.InitializeRules();
    }

    public override bool Equals(object obj)
    {
      if (this != obj)
        goto label_48;
label_47:
      return true;
label_48:
      if (!base.Equals(obj) || this.GetType() != obj.GetType())
        return false;
      SwiftBlock2Output swiftBlock2Output = (SwiftBlock2Output) obj;
      if (this.MessageInputReference.InputDate == null)
      {
        if (swiftBlock2Output.MessageInputReference.InputDate != null)
          return false;
      }
      else if (this.MessageInputReference.InputDate != swiftBlock2Output.MessageInputReference.InputDate)
        return false;
      if (this.MessageInputReference.LTAddress == null)
      {
        if (swiftBlock2Output.MessageInputReference.LTAddress != null)
          return false;
      }
      else if (this.MessageInputReference.LTAddress != swiftBlock2Output.MessageInputReference.LTAddress)
        return false;
      if (this.MessageInputReference.SequenceNumber == null)
      {
        if (swiftBlock2Output.MessageInputReference.SequenceNumber != null)
          return false;
      }
      else if (this.MessageInputReference.SequenceNumber != swiftBlock2Output.MessageInputReference.SequenceNumber)
        return false;
      if (this.MessageInputReference.SessionNumber == null)
      {
        if (swiftBlock2Output.MessageInputReference.SessionNumber != null)
          return false;
      }
      else if (this.MessageInputReference.SessionNumber != swiftBlock2Output.MessageInputReference.SessionNumber)
        return false;
      if (this.MessagePriority == null)
      {
        if (swiftBlock2Output.MessagePriority != null)
          return false;
      }
      else if (this.MessagePriority != swiftBlock2Output.MessagePriority)
        return false;
      if (this.MessageType == null)
      {
        if (swiftBlock2Output.MessageType != null)
          return false;
      }
      else if (this.MessageType != swiftBlock2Output.MessageType)
        return false;
      if (this.ReceiverOutputDate == null)
      {
        if (swiftBlock2Output.ReceiverOutputDate != null)
          return false;
      }
      else if (this.ReceiverOutputDate != swiftBlock2Output.ReceiverOutputDate)
        return false;
      if (this.ReceiverOutputTime == null)
      {
        if (swiftBlock2Output.ReceiverOutputTime != null)
          return false;
      }
      else if (this.ReceiverOutputTime != swiftBlock2Output.ReceiverOutputTime)
        return false;
      if (this.InputTime == null)
      {
        if (swiftBlock2Output.InputTime != null)
          return false;
        else
          goto label_47;
      }
      else if (this.InputTime != swiftBlock2Output.InputTime)
        return false;
      else
        goto label_47;
    }

    public override int GetHashCode()
    {
      return 31 * (31 * (31 * (31 * (31 * (31 * (31 * (31 * (31 * base.GetHashCode() + (this.MessageInputReference.InputDate != null ? this.MessageInputReference.InputDate.GetHashCode() : 0)) + (this.MessageInputReference.LTAddress != null ? this.MessageInputReference.LTAddress.GetHashCode() : 0)) + (this.MessageInputReference.SequenceNumber != null ? this.MessageInputReference.SequenceNumber.GetHashCode() : 0)) + (this.MessageInputReference.SessionNumber != null ? this.MessageInputReference.SessionNumber.GetHashCode() : 0)) + (this.MessagePriority != null ? this.MessagePriority.GetHashCode() : 0)) + (this.MessageType != null ? this.MessageType.GetHashCode() : 0)) + (this.ReceiverOutputDate != null ? this.ReceiverOutputDate.GetHashCode() : 0)) + (this.ReceiverOutputTime != null ? this.ReceiverOutputTime.GetHashCode() : 0)) + (this.InputTime != null ? this.InputTime.GetHashCode() : 0);
    }

    internal override XmlNode ToXml(XmlDocument doc)
    {
      XmlElement element1 = doc.CreateElement("ApplicationHeaderBlock_Output");
      XmlElement element2 = doc.CreateElement("MessageType");
      element2.InnerText = this.MessageType;
      element1.AppendChild((XmlNode) element2);
      XmlElement element3 = doc.CreateElement("InputTime");
      element3.InnerText = this.InputTime;
      element1.AppendChild((XmlNode) element3);
      XmlElement element4 = doc.CreateElement("MessageInputReference");
      XmlElement element5 = doc.CreateElement("InputDate");
      element5.InnerText = this.MessageInputReference.InputDate;
      element4.AppendChild((XmlNode) element5);
      XmlElement element6 = doc.CreateElement("LTAddress");
      element6.InnerText = this.MessageInputReference.LTAddress;
      element4.AppendChild((XmlNode) element6);
      XmlElement element7 = doc.CreateElement("SessionNumber");
      element7.InnerText = this.MessageInputReference.SessionNumber;
      element4.AppendChild((XmlNode) element7);
      XmlElement element8 = doc.CreateElement("SequenceNumber");
      element8.InnerText = this.MessageInputReference.SequenceNumber;
      element4.AppendChild((XmlNode) element8);
      element1.AppendChild((XmlNode) element4);
      XmlElement element9 = doc.CreateElement("ReceiverOutputDate");
      element9.InnerText = this.ReceiverOutputDate;
      element1.AppendChild((XmlNode) element9);
      XmlElement element10 = doc.CreateElement("ReceiverOutputTime");
      element10.InnerText = this.ReceiverOutputTime;
      element1.AppendChild((XmlNode) element10);
      XmlElement element11 = doc.CreateElement("MessagePriority");
      element11.InnerText = this.MessagePriority;
      element1.AppendChild((XmlNode) element11);
      return (XmlNode) element1;
    }

    public override string GetValue()
    {
      if (this.IsEmpty)
        return (string) null;
      StringBuilder stringBuilder = new StringBuilder("O");
      if (this.MessageType != null)
        stringBuilder.Append(this.MessageType);
      if (this.InputTime != null)
        stringBuilder.Append(this.InputTime);
      if (this.MessageInputReference.GetValue() != null)
        stringBuilder.Append(this.MessageInputReference.GetValue());
      if (this.ReceiverOutputDate != null)
        stringBuilder.Append(this.ReceiverOutputDate);
      if (this.ReceiverOutputTime != null)
        stringBuilder.Append(this.ReceiverOutputTime);
      if (this.MessagePriority != null)
        stringBuilder.Append(this.MessagePriority);
      return ((object) stringBuilder).ToString();
    }

    public override bool SetValue(string value, List<ValidationError> parseError)
    {
      bool flag = true;
      int length = value.Length;
      switch (length)
      {
        case 46:
        case 48:
        case 47:
        case 49:
          int index = 0;
          if (value.StartsWith("2:O"))
            index = 2;
          switch (length - index)
          {
            case 46:
            case 47:
              if ((int) value[index] != 79)
              {
                if (parseError != null)
                {
                  ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, "Value must match: O<mt><time><mir><date><time>[<pri>]");
                  parseError.Add(validationError);
                }
                flag = false;
              }
              int start1 = index + 1;
              int size1 = 3;
              this.MessageType = this.GetValuePart(value, start1, size1);
              int start2 = start1 + size1;
              int size2 = 4;
              this.InputTime = this.GetValuePart(value, start2, size2);
              int start3 = start2 + size2;
              int size3 = 28;
              this.MessageInputReference.SetValue(this.GetValuePart(value, start3, size3), parseError);
              int start4 = start3 + size3;
              int size4 = 6;
              this.ReceiverOutputDate = this.GetValuePart(value, start4, size4);
              int start5 = start4 + size4;
              int size5 = 4;
              this.ReceiverOutputTime = this.GetValuePart(value, start5, size5);
              int start6 = start5 + size5;
              int size6 = 1;
              this.MessagePriority = this.GetValuePart(value, start6, size6);
              int num = start6 + size6;
              return flag;
            default:
              if (parseError != null)
              {
                ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, "Value must match: O<mt><time><mir><date><time>[<pri>]");
                parseError.Add(validationError);
              }
              flag = false;
              goto case 46;
          }
        default:
          if (parseError != null)
          {
            ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, ((object) new StringBuilder().Append("expected a string Value of 46 and up to 49 chars and obtained a ").Append(length).Append(" chars string: '").Append(value).Append("'")).ToString());
            parseError.Add(validationError);
          }
          flag = false;
          goto case 46;
      }
    }
  }
}
